<?php
namespace app\client\controller;
use app\common\lib\GatewayCurl;

class Testcharge extends Base {
    
    public function __construct() {
        parent::__construct();
    }
    
    //充电
    public function charge() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $params = request()->post();
        $chargeType = $params['chargeType'];
        $quantity = $params['quantity'];
        $portNo = isset($params['portNo']) ? intval($params['portNo']) : 1;
        $deviceNo = isset($params['deviceNo']) ? intval($params['deviceNo']) : 0;
        
        $time = time();
        
        $device = model('Device')->findOne(['device_no' => $deviceNo]);
        
        if(($device['heart_time'] + 300) < $time) {
            return errorResult('设备已掉线');
        }
        
        
        $orderNo = getOrderNo($this->userid);
        $orderData = [
            'user_id' => $this->userid,
            'order_no' => $orderNo,
            'device_no' => $deviceNo,
            'port_no' => $portNo,
            'charge_type' => $chargeType,
            'create_time' => $time
        ];
        
        $orderId = model('TestChargeOrder')->add($orderData);
        if(!$orderId){
            return errorResult();
        }
        
        if($chargeType == 2) { //计量
            $quantity = $quantity * 1000;
        }
        
        $curl = new GatewayCurl($device['gateway']);
        $result = $curl->send('startCharge', ['orderId' => 't' . $orderId, 'deviceId' => $device['sim_iccid'], 'chargeType' => $chargeType, 'portNo' => $portNo, 'quantity' => $quantity]);
        if($result['success'] == false) { //发送失败,退款
            return errorResult('充电失败');
        }
        
        return successResult(['orderId' => $orderId]);
        
    }
    
    public function chargeResult() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $orderId = request()->post('id', 0, 'intval');
        if($orderId <= 0) {
            return errorResult('cc');
        }
        
        $order = model('TestChargeOrder')->findOne(['order_id' => $orderId], ['status']);
        if(empty($order)) {
            return errorResult('dd');
        }
        
        return successResult(['status' => $order['status']]);
    }
    
    
    //增加充电时间
    public function extendCharge() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $params = request()->post();
        
        $orderId = intval($params['id']);
        $chargeType = $params['chargeType'];
        $quantity = $params['quantity'];
        $portNo = isset($params['portNo']) ? intval($params['portNo']) : 1;
        $deviceNo = isset($params['deviceNo']) ? intval($params['deviceNo']) : 0;

        $time = time();
        
        $device = model('Device')->findOne(['device_no' => $deviceNo]);

        $orderNo = getOrderNo($this->userid);
        $orderData = [
            'order_no' => $orderNo,
            'extend_quantity' => $quantity,
            'charge_type' => $chargeType,
            'charge_order_id' => $orderId,
            'create_time' => $time
        ];

        $orderId = model('TestExtendChargeOrder')->add($orderData);
        
        if($chargeType == 2) { //计量
            $quantity = $quantity * 1000;
        }
        
        $curl = new GatewayCurl($device['gateway']);
        $result = $curl->send('addCharge', ['orderId' => 't' . $orderId, 'deviceId' => $device['sim_iccid'], 'portNo' => $portNo, 'chargeType' => $chargeType, 'quantity' => $quantity]);
        if($result['success'] == false) { //发送失败,退款
            return errorResult('增加充电失败');
        }
        
        return successResult(['orderId' => $orderId]);
    }
    
    
    public function extendChargeResult() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        
        $orderId = request()->post('id', 0, 'intval');
        if($orderId <= 0) {
            return errorResult('cc');
        }
        $order = model('ExtendChargeOrder')->findOne(['order_id' => $orderId], ['status']);
        if(empty($order)) {
            return errorResult('dd');
        }
        
        return successResult(['status' => $order['status']]);
    }
    
    //停止充电
    public function stopCharge() {
        if($this->noLogin()) {
            return $this->noLoginReturn();
        }
        $params = request()->post();
        $orderId = intval($params['id']);
        $chargeType = $params['chargeType'];
        $quantity = $params['quantity'];
        $portNo = isset($params['portNo']) ? intval($params['portNo']) : 1;
        $deviceNo = isset($params['deviceNo']) ? intval($params['deviceNo']) : 0;
        
        $device = model('Device')->findOne(['device_no' => $deviceNo], ['gateway', 'sim_iccid']);
        
        $curl = new GatewayCurl($device['gateway']);
        $result = $curl->send('stopCharge', ['deviceId' => $device['sim_iccid'], 'portNo' => $portNo]);
        if($result['success'] == false) {
            return errorResult();
        }
        
        return successResult();
    }
    

    public function getOrderList() {
        $list = model('TestChargeOrder')->where(['user_id' => $this->userid])->field(['order_id', 'port_no', 'status', 'start_time', 'charge_type'])->order('order_id desc')->limit(20)->select();
        return successResult(['list' => $list]);
    }
}

















